<template>
  <div class="admin-product-management">
    <el-card>
      <template #header>
        <div class="card-header">
          <h2>产品管理</h2>
          <el-button type="primary" @click="handleAddProduct">新建产品</el-button>
        </div>
      </template>

      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="产品名称">
          <el-input v-model="searchForm.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="searchForm.category" placeholder="请选择类别">
            <el-option label="全部" value="" />
            <el-option label="蔬菜" value="vegetables" />
            <el-option label="水果" value="fruits" />
            <el-option label="肉类" value="meat" />
            <el-option label="海鲜" value="seafood" />
          </el-select>
        </el-form-item>
        <el-form-item label="库存状态">
          <el-select v-model="searchForm.stock_status" placeholder="请选择库存状态">
            <el-option label="全部" value="" />
            <el-option label="充足" value="sufficient" />
            <el-option label="低库存" value="low" />
            <el-option label="缺货" value="out_of_stock" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 产品列表 -->
      <el-table :data="products" style="width: 100%">
        <el-table-column prop="product_code" label="产品编号" width="120" />
        <el-table-column prop="name" label="产品名称" width="150" />
        <el-table-column prop="category" label="类别" width="100">
          <template #default="{ row }">
            <el-tag>{{ getCategoryText(row.category) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="price" label="单价" width="100">
          <template #default="{ row }">
            ¥{{ row.price.toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column prop="stock" label="库存" width="100" />
        <el-table-column prop="stock_status" label="库存状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStockStatusType(row.stock_status)">
              {{ getStockStatusText(row.stock_status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="{ row }">
            <el-button type="primary" link @click="handleEdit(row)">编辑</el-button>
            <el-button type="success" link @click="handleViewDetails(row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新建产品' : '编辑产品'"
      width="50%"
    >
      <el-form :model="productForm" label-width="100px">
        <el-form-item label="产品名称">
          <el-input v-model="productForm.name" />
        </el-form-item>
        <el-form-item label="类别">
          <el-select v-model="productForm.category">
            <el-option label="蔬菜" value="vegetables" />
            <el-option label="水果" value="fruits" />
            <el-option label="肉类" value="meat" />
            <el-option label="海鲜" value="seafood" />
          </el-select>
        </el-form-item>
        <el-form-item label="单价">
          <el-input-number v-model="productForm.price" :precision="2" :step="0.1" :min="0" />
        </el-form-item>
        <el-form-item label="库存">
          <el-input-number v-model="productForm.stock" :min="0" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="productForm.description" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

// 搜索表单
const searchForm = ref({
  name: '',
  category: '',
  stock_status: ''
})

// 产品列表数据
const products = ref([
  {
    id: 1,
    product_code: 'PRD001',
    name: '新鲜西红柿',
    category: 'vegetables',
    price: 5.99,
    stock: 100,
    stock_status: 'sufficient',
    description: '新鲜采摘的西红柿，口感好',
    created_at: '2024-03-01 09:00:00'
  },
  {
    id: 2,
    product_code: 'PRD002',
    name: '进口香蕉',
    category: 'fruits',
    price: 12.99,
    stock: 50,
    stock_status: 'low',
    description: '进口香蕉，品质保证',
    created_at: '2024-03-01 10:30:00'
  },
  {
    id: 3,
    product_code: 'PRD003',
    name: '新鲜三文鱼',
    category: 'seafood',
    price: 158.00,
    stock: 0,
    stock_status: 'out_of_stock',
    description: '新鲜三文鱼，冷链配送',
    created_at: '2024-03-01 11:15:00'
  }
])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(3)

// 对话框相关
const dialogVisible = ref(false)
const dialogType = ref('add')
const productForm = ref({
  name: '',
  category: 'vegetables',
  price: 0,
  stock: 0,
  description: ''
})

// 获取类别文本
const getCategoryText = (category) => {
  const textMap = {
    vegetables: '蔬菜',
    fruits: '水果',
    meat: '肉类',
    seafood: '海鲜'
  }
  return textMap[category] || '未知'
}

// 获取库存状态样式
const getStockStatusType = (status) => {
  const typeMap = {
    sufficient: 'success',
    low: 'warning',
    out_of_stock: 'danger'
  }
  return typeMap[status] || 'info'
}

// 获取库存状态文本
const getStockStatusText = (status) => {
  const textMap = {
    sufficient: '充足',
    low: '低库存',
    out_of_stock: '缺货'
  }
  return textMap[status] || '未知'
}

// 处理搜索
const handleSearch = () => {
  console.log('搜索条件:', searchForm.value)
  // 这里实现搜索逻辑
}

// 处理重置
const handleReset = () => {
  searchForm.value = {
    name: '',
    category: '',
    stock_status: ''
  }
}

// 处理新建产品
const handleAddProduct = () => {
  dialogType.value = 'add'
  productForm.value = {
    name: '',
    category: 'vegetables',
    price: 0,
    stock: 0,
    description: ''
  }
  dialogVisible.value = true
}

// 处理编辑
const handleEdit = (row) => {
  dialogType.value = 'edit'
  productForm.value = { ...row }
  dialogVisible.value = true
}

// 处理查看详情
const handleViewDetails = (row) => {
  console.log('查看产品详情:', row)
}

// 处理提交
const handleSubmit = () => {
  console.log('提交产品:', productForm.value)
  ElMessage.success(dialogType.value === 'add' ? '新建成功' : '更新成功')
  dialogVisible.value = false
}

// 处理分页大小变化
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

// 处理页码变化
const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}

onMounted(() => {
  // 这里可以加载实际数据
  console.log('产品管理页面加载完成')
})
</script>

<style scoped>
.admin-product-management {
  padding: 24px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style> 